package com.sinochem.sales.admin.controller;

import com.sinochem.sales.admin.api.to.UserTO;
import com.sinochem.sales.admin.api.vo.MenuVO;
import com.sinochem.sales.admin.api.vo.UserVO;
import com.sinochem.sales.admin.service.IUserService;
import com.sinochem.sales.common.entity.Result;
import com.sinochem.sales.common.util.AssertUtils;
import com.sinochem.sales.common.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.exception.BaseException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xuan😴🐶🐱🐽🌺  <a href="zhangshixuanj@163.com">联系作者</a>
 * @version Revision:v1.0,Date: 2018/4/16 14:30
 * @project sinochem-ubmp-registry
 * @description
 * @Modification Date: 2018/4/16 14:30  {填写修改说明}
 */
@Slf4j
@RestController
@RequestMapping("user")
@Api(tags = "用户管理")
public class UserController {


    @Autowired
    private SqlToyLazyDao sqlToyLazyDao;
    @Autowired
    IUserService iUserService;

    /**
     * 通过用户名查询用户、角色信息
     *
     * @param userName 用户名
     * @return UserVo
     */
    @ApiOperation(value = "通过用户名查询用户、角色信息", notes = "通过用户名查询用户、角色信息")
    @GetMapping("/findUserByUserName")
    public Result<UserTO> findUserByUserName(@RequestParam("userName") String userName) {
        UserVO userVO;
        try {
            userVO = (UserVO) sqlToyLazyDao.loadBySql("find_user_by_userName", new String[]{"userName"},
                    new Object[]{userName},
                    UserVO.class);
        } catch (Exception e) {
            log.error("查询用户失败", e);
            throw new RuntimeException("查询用户失败", e);
        }
        if (userVO == null) {
            return Result.error("没有查询到用户");
        }
        UserTO userTO = new UserTO();

        BeanUtils.copyProperties(userVO, userTO);

        try {
            List<MenuVO> menuVOS = sqlToyLazyDao.findBySql("find_permission", new String[]{"userId"},
                    new Object[]{userTO.getUserId()},
                    MenuVO.class);
            userTO.setMenuList(menuVOS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("查询所有权限失败:", e);
        }
        return new Result<>(userTO);
    }


    /**
     * 根据token获取当前UID
     *
     * @return
     */
    @ApiOperation(value = "通过用户名查询用户、角色信息", notes = "通过用户名查询用户、角色信息")
    @GetMapping("getUserId")
    public Result<UserTO> getUserId() {
        UserTO userTO = new UserTO();
        userTO.setUsername(UserUtils.getUser());
        return new Result<>(userTO);
    }


    /**
     * 通过手机号查询用户、角色信息
     *
     * @param mobile 手机号
     * @return UserVo
     */
    @GetMapping("/findUserByMobile/{mobile}")
    public Result<UserTO> findUserByMobile(@PathVariable("mobile") String mobile) {
        UserVO userVO;
        try {
            userVO = (UserVO) sqlToyLazyDao.loadBySql("find_user_by_mobile", new String[]{"mobile"},
                    new Object[]{mobile},
                    UserVO.class);
        } catch (Exception e) {
            log.error("查询用户失败", e);
            throw new BaseException("查询用户失败", e);
        }
        UserTO userTO = new UserTO();

        BeanUtils.copyProperties(userVO, userTO);
        return new Result<>(userTO);
    }


    /**
     * 根据用户id修改用户密码
     *
     * @param newPwd
     * @return
     */
    @ApiOperation(value = "通过uid修改用戶密碼", notes = "通过uid修改用戶密碼")
    @GetMapping("/updatePasswordById")
    public Result<String> updatePasswordById(@RequestParam("uid") String uid, @RequestParam("newPwd") String newPwd) {
        AssertUtils.notBlank(newPwd,"密码不能为空");
        try {
            boolean flag = iUserService.updateUserPwd(uid, newPwd);
            if (flag) {
                return Result.success("修改商户信息成功");
            }
            return Result.error("修改商户信息失败");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改商户信息失败");
            throw new BaseException("修改商户信息失败");
        }
    }


    /**
     * 修改用户信息
     *
     * @param userTO
     * @return
     */
    @ApiOperation(value = "修改用戶信息", notes = "修改用戶信息")
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserTO userTO) {
        try {
            boolean flag = iUserService.updateUserInfo(userTO);
            if (flag) {
                return Result.success("修改商户信息成功");
            }
            return Result.error("修改商户信息失败");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改商户信息失败");
            throw new BaseException("修改商户信息失败");
        }
    }
}
